//
//  TVChannelsViewModel.swift
//  StarTimesON
//
//  Created by Toj on 12/8/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

class TVChannelsViewModel: NSObject {
    // 后端返回的数据
    private(set) var channels: [STChannelCellModel] = []
    // 展示的渠道列表
    private(set) var showChannels: [STChannelCellModel] = []
    // 钱包渠道
    private(set) var walletChannel: STChannelCellModel?
    
    private(set) var defaultChannel: STChannelCellModel?
    
    // 关闭时候滑动的位置, 展示的时候确定组位置
    var sectionIndex: Int = 0
    
    // 选中的规格
    public var selectedChannel: STChannelCellModel? { willSet {
        selectedChannel?.isSelected = false
        newValue?.isSelected = true
    } }
    
    // 当前状态
    var isOpened: Bool = false { didSet {
        if isOpened {
            // 打开状态展示全部
            showChannels = channels
            return
        }
        
        // 收起状态展示当前选择的
        guard let channel = selectedChannel else {
            showChannels = channels
            return
        }
        
        // 如果是银行卡支付, 需要添加依附支付方式
        guard let dependChannel = channel.dependChannel else {
            showChannels = [channel]
            return
        }
        showChannels = [dependChannel, channel]
    } }
    
    // 根据当前状态返回列表数量
    var showNumber: Int {
        return showChannels.count
    }
    
    init(payChannels: [PayChannelDto]) {
        super.init()
        
        var channelViewModels: [STChannelCellModel] = []
        var defaultChannel_: STChannelCellModel?
        let isLogin = STUserModel.shared.isLogin()
        payChannels.forEach { payChannel in
            
            // 非绑卡支付方式
            guard payChannel.isSupportCardBind && isLogin else {
                let channelViewModel = STChannelCellModel(payChannel: payChannel)
                channelViewModels.append(channelViewModel)
                if defaultChannel_ == nil { defaultChannel_ = channelViewModel }
                if channelViewModel.isEWallet { walletChannel = channelViewModel }
                return
            }

            // 绑卡支付方式
            let channelViewModel = STChannelCellModel(payChannel: payChannel)
            channelViewModels.append(channelViewModel)
            if let list = payChannel.payChannelCardAuthDtoList, list.count > 0 {
                let cardArr = payCardToViewModel(payChannelCards: list, channelModel: channelViewModel)
                if defaultChannel_ == nil { defaultChannel_ = cardArr.first }
                channelViewModels += cardArr
            }
            // Add支付方式
            channelViewModels.append(addCarViewModel(payChannel: payChannel))
        }

        channels = channelViewModels
        defaultChannel = defaultChannel_
        // 如果只有一个支付渠道, 去掉虚线
        guard channels.count <= 1 else { return }
        defaultChannel?.isOnlyOne = true
    }
    
    func payCardToViewModel(payChannelCards: [PayChannelCardAuthDto], channelModel: STChannelCellModel) -> [STChannelCellModel] {
        var channels:[STChannelCellModel] = []
        payChannelCards.forEach { dto in
            let channel = STChannelCellModel(payChannelCard: dto, channelModel: channelModel)
            channels.append(channel)
        }
        return channels
    }
    
    func addCarViewModel(payChannel: PayChannelDto) -> STChannelCellModel {
        let addCarCell = STChannelCellModel(payChannel: payChannel)
        addCarCell.isAddCarCell = true
        addCarCell.isCarCell = true
        addCarCell.name = "add_another_card_pay".localized()
        return addCarCell
    }
    
    func updateWalletChannelInfo() {
        walletChannel?.eWalletAccount(STSPayAccount.current.payAccount)
    }
    
    func reloadWalletChannel(payAmount: Double) {
        guard let walletChannel = walletChannel else {
            return
        }
        walletChannel.actualPayAmount = payAmount
    }
}
